from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import io
def addTitle(img,path,words,config):
    # 打开一个jpg图像文件，注意是当前路径:
    #im = Image.open('title.jpg').convert('RGB')
    
    title = Image.open(config.titleImg).convert('RGB')
    draw = ImageDraw.Draw(title)
    # x=192 ,y =20   
    url = words if len(words) <= config.wordShowLen else (words[0:config.wordShowSplit] + "...")
    wordSize = config.wordSize #文字大小
    wordCss  = config.wordCss #字体文件   行楷    
    font = ImageFont.truetype(wordCss,wordSize)
    draw.text([config.wordX,config.wordY],url, fill = tuple(eval(config.wordRgb)), font=font)
    # im.show()
    #result = Image.open(img.decode('ascii', 'ignore').encode('utf-8')).convert('RGB')
    target = Image.new('RGB', size = (config.imgWidth,config.imgHeight + config.titleImgHeight))   
    target.paste(title,(0,0,config.imgWidth,config.titleImgHeight))
    #img1 = Image.frombytes('RGB',(800,600),img)
    imgIo = io.BytesIO(img)
    target.paste(Image.open(imgIo),(0,config.titleImgHeight,config.imgWidth,(config.imgHeight + config.titleImgHeight)))
    target.save(path)
